'''

@author: wyndem
@Emil:   wyndem@qq.com
@FileName: createCard.py
@Time: 2019/2/22 16:44
@Software: PyCharm
@Description: 

'''
import time

from db.sqlLiteUtil import Card, db
from util.PasswordGenerator import getKey

if __name__ == '__main__':
    num =int( input('生成卡密数量：'))
    money = input('生成的金额：')
    data = [{
        'key':getKey(),
        'value':money,
        'createDate':time.strftime("%F"),
        'available':True
    } for i in range(num)]
    print(data)

    with db.atomic():
        for i in range(0, num, 100):
           Card.insert_many(data[i:i + 100]).execute()
        with open(money + ".txt", "a", encoding='utf-8') as f:
            head ='------' + time.strftime("%F-%H:%M:%S") + '------' + '共' + str(len(data)) + '个卡密' + '-------\n'
            end= '\n\n-------------End------------------------\n\n\n\n\n\n\n\n'
            f.write(head)
            print(head)
            for i in data:
                f.write(i['key'] + '\n')
                print(i['key'])
            f.write(end)
            print(end)
